/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 */

package org.apache.ranger.obs.security.token;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.obs.security.RangerAuthorizeProvider;
import org.apache.hadoop.security.token.Token;
import org.apache.ranger.obs.server.DistributedMiniServerBaseTest;
import org.apache.ranger.obs.server.ServerConstants;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * description
 *
 * @since 2021-10-12
 */
public class DelegationRenewerTest extends DistributedMiniServerBaseTest {
    Token<DelegationTokenIdentifier> token;

    Configuration client_conf;

    @Before
    public void setUp() throws IOException {
        String principal = "ranger/hadoop@EXAMPLE.COM";
        RangerAuthorizeProvider authorizer = new RangerAuthorizeProvider();
        client_conf = new Configuration();
        client_conf.set("ranger.obs.service.rpc.address", "127.0.0.1:26901;127.0.0.1:26902");
        client_conf.set("ranger.obs.service.kerberos.principal", principal);
        authorizer.init(client_conf);
        token = (Token<DelegationTokenIdentifier>) authorizer.getDelegationToken("ranger");
    }

    @After
    public void tearDown() {
    }

    @Test
    public void testHandleKind() {
        Assert.assertEquals("testHandleKind", "RANGER_OBS_SERVICE_DELEGATION_TOKEN", token.getKind().toString());
    }

    @Test
    public void testIsManaged() throws IOException {
        Assert.assertTrue("testIsManaged", token.isManaged());
    }

    @Test
    public void testRenew() throws IOException, InterruptedException {
        long start = System.currentTimeMillis();
        long renew = token.renew(client_conf);
        Assert.assertTrue("testRenew", start + ServerConstants.DEFAULT_RANGER_OBS_SERVICE_DT_RENEW_INTERVAL <= renew);
    }

    @Test
    public void testCancel() throws IOException, InterruptedException {
        token.cancel(client_conf);
    }
}